package com.kaifamiao.ajax.controller;

import org.tinylog.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {

    @Override
    protected void doHead(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger.info( "HEAD" + request.getRequestURI() );
        // 接收来自客户端的请求参数
        String name = request.getParameter("name");
        Logger.info( "name: " + name );
        response.setContentType( "text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println( "{\"success\": true, \"message\": \"成功\"}");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger.info( "GET: " + request.getRequestURI() );
        // 接收来自客户端的请求参数
        String name = request.getParameter("name");
        Logger.info( "name: " + name );

        response.setContentType( "text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println( "<h5>" );
        out.println( LocalDateTime.now() );
        out.println( "</h5>" );
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger.info( "POST" + request.getRequestURI() );
        // 接收来自客户端的请求参数
        String name = request.getParameter("name");
        Logger.info( "name: " + name );
        response.setContentType( "text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println( "{\"success\": true, \"message\": \"成功\"}");
    }

    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger.info( "PUT" + request.getRequestURI() );
        // 接收来自客户端的请求参数
        String name = request.getParameter("name");
        Logger.info( "name: " + name );
        response.setContentType( "text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println( "{\"success\": true, \"message\": \"成功\"}");
    }

    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Logger.info( "DELETE" + request.getRequestURI() );
        // 接收来自客户端的请求参数
        String name = request.getParameter("name");
        Logger.info( "name: " + name );
        response.setContentType( "text/plain;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println( "{\"success\": true, \"message\": \"成功\"}");
    }

}
